package com.klikmee.sms.core.base.handlers;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.util.DeliveryReports;
import com.klikmee.sms.core.base.util.States;
import com.klikmee.sms.core.model.BulkMessage;

/**
 * Updates state of the bulk message. Moves state of bulk message to either
 * delivered or undelivered.
 * 
 * @author makcro
 * 
 */
public class BulkMessageDeliveryReportHandler extends AbstractDeliveryHandler<BulkMessage> {

	/**
	 * Logger instance
	 */
	private Logger	logger	= Logger.getLogger( BulkMessageDeliveryReportHandler.class );

	@Override
	public void handle(BulkMessage message) {
		// whitch delivery status of bulk message
		Integer deliveryStatus = message.getDeliveryStatus();
		switch (deliveryStatus) {
		case DeliveryReports.DELIVERED:
			onBulkMessageDelivered(message);
			break;
		
		default:
			onDefaultStatus(message);
			break;
		}
		
		// persist changes to database
		this.bulkMessageDAO.edit( message );
	}

	private void onDefaultStatus(BulkMessage message) {
		// update state to undelivered
		message.setState( States.BulkMessage.UNDELIVERED );
		
	}

	private void onBulkMessageDelivered(BulkMessage message) {
		message.setState( States.BulkMessage.DELIVERED );
		
	}

}
